package com.whmnx.chitchat.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.tencent.qcloud.tim.uikit.component.photoview.PhotoView;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.ShowImagesAdapter;
import com.whmnx.chitchat.utils.ImageUtil;


import java.util.ArrayList;
import java.util.List;




public class ShowImagesDialog extends Dialog implements View.OnClickListener {

    private View mView;
    private Context mContext;
    private ViewPager mViewPager;
    private TextView mIndexText;
    private List<String> mImgUrls;
    private List<String> mTitles;
    private List<View> mViews;
    private ShowImagesAdapter mAdapter;

    private FrameLayout mClose;
    private FrameLayout mShareOrSave;
    private FrameLayout fl_conversation;
    private RelativeLayout mRlPhotoTop;
    private RelativeLayout mRlPhotoBottom;
    private LinearLayout ll_save_or_share;
    private TextView tv_share;
    private TextView tv_save;
    private TextView tv_cancel;
    private TextView tv_like_count;
    private TextView tv_write_count;
    private TextView tv_content;

    private LinearLayout ll_like;
    private LinearLayout ll_write;

    private ImageView iv_like;

    private boolean isLike;

    private int position;
    private String content;

    public ShowImagesDialog(@NonNull Context context, List<String> imgUrls, int position) {
        super(context, R.style.AppTheme);
        this.mContext = context;
        this.mImgUrls = imgUrls;
        this.position = position;
        initView();
        initData();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        mView = View.inflate(mContext, R.layout.dialog_images_brower, null);

        mViewPager = mView.findViewById(R.id.vp_images);
        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    default:
                }
                return false;
            }
        });

        mClose = mView.findViewById(R.id.fl_close);
        mIndexText = mView.findViewById(R.id.tv_image_index);
        mRlPhotoTop = mView.findViewById(R.id.rl_photo_top);

        mClose.setOnClickListener(this);
        mTitles = new ArrayList<>();
        mViews = new ArrayList<>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mView);
        Window window = getWindow();
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = 0;
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
        mViewPager.setCurrentItem(position);
        mIndexText.setText(position + 1 + "/" + mImgUrls.size());
    }

    private void initData() {
        for (int i = 0; i < mImgUrls.size(); i++) {
            PhotoView photoView = new PhotoView(mContext);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            photoView.setLayoutParams(layoutParams);
            photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
         //   photoView.setOnPhotoTapListener(listener);

            ImageUtil.setImage(photoView,mImgUrls.get(i));

         //   Glide.with(mContext).load(mImgUrls.get(i)).into(photoView);
             mViews.add(photoView);
           //   mTitles.add(i + "");
        }

        mAdapter = new ShowImagesAdapter(mViews, mTitles);
        mViewPager.setAdapter(mAdapter);
        mIndexText.setText(1 + "/" + mImgUrls.size());
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mIndexText.setText(position + 1 + "/" + mImgUrls.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fl_close:
                dismiss();
            default:
                break;
        }
    }
}
